package com.yzt.plugin_widget;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

/**
 * author : bob
 * date   : 2022/11/28
 * desc   : 项目标题栏
 */
public class AppTitleBar extends LinearLayout {

    private LinearLayout mTopLayout;

    private ImageView mLeftIv;

    private RelativeLayout mLeftLayout;

    private Resources mRes;

    private TextView mTitleContentTv;

    private View mTitleView = null;

    private Context mContext;

    private RelativeLayout mRightLayout;

    private TextView mTitleRightTv;

    private ImageView mTitleRightIv;

    public View viewLine;

    private View StatusBarLayout;

    public AppTitleBar(Context context) {
        super(context);
        init(context);
    }

    public AppTitleBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        mRes = getResources();
        mContext = context;
        initData(context);
    }

    /**
     * 初始化页面数据
     */
    private void initData(Context context) {
        mTitleView = LayoutInflater.from(context).inflate(R.layout.view_title_bar, this);
        mTopLayout = mTitleView.findViewById(R.id.layout_top);
        mLeftIv = mTitleView.findViewById(R.id.iv_left);
        mLeftLayout = mTitleView.findViewById(R.id.rl_left);
        mTitleContentTv = mTitleView.findViewById(R.id.tv_title_content);

        mRightLayout = mTitleView.findViewById(R.id.rl_right);
        mTitleRightTv = mTitleView.findViewById(R.id.tv_title_right);
        mTitleRightIv = mTitleView.findViewById(R.id.iv_title_right);
        viewLine = mTitleView.findViewById(R.id.view_line);
        if (viewLine != null) {
            viewLine.setVisibility(GONE);
        }
        StatusBarLayout = findViewById(R.id.statubar_layout);
    }

    private void hideView(View view) {
        if (view.getVisibility() == View.VISIBLE) {
            view.setVisibility(View.GONE);
        }
    }

    private void showView(View view) {
        if (view.getVisibility() == View.GONE) {
            view.setVisibility(View.VISIBLE);
        }
    }

    public void setTitle(Object title) {
        showView(mTitleView);
        showView(mTitleContentTv);
        showView(mLeftIv);
        showView(mLeftLayout);
        hideView(mRightLayout);
        setTitleContent(title);
    }

    public void setTitle(Object title, Integer rightDrawable) {
        showView(mTitleView);
        showView(mTitleContentTv);
        showView(mLeftIv);
        showView(mLeftLayout);
        showView(mRightLayout);
        setRightDrawable(rightDrawable);
    }

    public void setLineViewVisibility(int visible) {
        viewLine.setVisibility(visible);
    }

    public void setTitleContent(Object title) {
        if (title == null) {
            return;
        }
        try {
            if (title instanceof String) {
                mTitleContentTv.setText((String) title);
            } else {
                mTitleContentTv.setText(mRes.getString((Integer) title));
            }
        } catch (Exception e) {
        }
    }

    public void setTitleColor(int colorId) {
        if (colorId > 0) {
            mTitleContentTv.setTextColor(ContextCompat.getColor(mContext, colorId));
        }
    }

    public void setRightContent(Object rightTitle) {
        showView(mTitleRightTv);
        showView(mRightLayout);
        if (rightTitle == null) {
            hideView(mRightLayout);
            hideView(mTitleRightTv);
        } else if (rightTitle instanceof String) {
            mTitleRightTv.setText((String) rightTitle);
        } else {
            mTitleRightTv.setText(mRes.getString((Integer) rightTitle));
        }
    }

    public void setRightDrawable(Integer rightDrawable) {
        showView(mRightLayout);
        showView(mTitleRightIv);
        if (rightDrawable == null) {
            hideView(mTitleRightIv);
            hideView(mRightLayout);
        } else {
            mTitleRightIv.setImageResource(rightDrawable);
        }
    }

    public void setLeftDrawable(Integer leftDrawable) {
        showView(mLeftIv);
        if (leftDrawable == null) {
            hideView(mLeftIv);
            hideView(mLeftLayout);
        } else if (leftDrawable instanceof Integer) {
            if (leftDrawable > 0) {
                mLeftIv.setImageResource(leftDrawable);
                showView(mLeftIv);
                showView(mLeftLayout);
            } else {
                hideView(mLeftLayout);
            }
        }
    }

    public void setLeftBtnListener(OnClickListener l) {
        mLeftLayout.setOnClickListener(l);
    }

    public void setRightBtnListener(OnClickListener l) {
        mRightLayout.setOnClickListener(l);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }

    public void setBackground(int colorId) {
        if (colorId > 0) {
            mTopLayout.setBackgroundColor(ContextCompat.getColor(mContext, colorId));
        }
    }

    @Override
    public void setBackgroundColor(int colorId) {
        setBackground(colorId);
    }

    public void setRightTextColor(int colorId) {
        if (colorId > 0) {
            mTitleRightTv.setTextColor(ContextCompat.getColor(mContext, colorId));
        }
    }
}
